package com.cqc.appmanager01;

import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by cui on 2017/7/28.
 */

public class MyAdapter extends BaseAdapter {
    private int appTypeView = 0;
    private int appInfoView = 1;

    private List<AppInfo> sysAppList;
    private List<AppInfo> userAppList;
    private ViewHolder holder;

    public MyAdapter(List<AppInfo> sysAppList, List<AppInfo> userAppList) {
        this.sysAppList = sysAppList;
        this.userAppList = userAppList;
    }

    @Override
    public int getCount() {
        return sysAppList.size() + userAppList.size() + 2;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == appTypeView) {//应用类别类型
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app_type, parent, false);
                holder = new ViewHolder();
                holder.tvAppType = (TextView) convertView.findViewById(R.id.tv_app_type);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (position == 0) {
                holder.tvAppType.setText("用户应用:" + userAppList.size());
            } else {
                holder.tvAppType.setText("系统应用:" + sysAppList.size());
            }
        } else {//应用详情类型
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app_info, parent, false);
                holder = new ViewHolder();
                holder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
                holder.tvAppLabel = (TextView) convertView.findViewById(R.id.tv_app_label);
                holder.tvAppSource = (TextView) convertView.findViewById(R.id.tv_app_is_in_phone);
                holder.tvSize = (TextView) convertView.findViewById(R.id.tv_app_size);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            AppInfo info;
            if (position > 0 && position <= userAppList.size()) {
                info = userAppList.get(position - 1);
            } else {
                info = sysAppList.get(position - 2 - userAppList.size());
            }
            holder.ivIcon.setImageDrawable(info.getIcon());
            holder.tvAppLabel.setText(info.getLabel());
            if (info.isInPhone()) {
                holder.tvAppSource.setText("手机内部");
            } else {
                holder.tvAppSource.setText("SD卡中");
            }
            holder.tvSize.setText(Formatter.formatFileSize(parent.getContext(), info.getSize()));
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position == (userAppList.size() + 1)) {
            return appTypeView;
        } else {
            return appInfoView;
        }
    }

    static class ViewHolder {
        ImageView ivIcon;
        TextView tvAppLabel;
        TextView tvAppSource;
        TextView tvSize;
        TextView tvAppType;
    }
}
